//
//  APIs.swift
//  BWNetworkKit_Example
//
//  Created by zhuxuhong on 2022/3/3.
//  Copyright © 2022 CocoaPods. All rights reserved.
//

import BWNetworkKit
import HandyJSON
import BWToolKit
import Foundation

typealias Requester<T: HandyJSON> = JSONData<T>
typealias FloatRequester = Requester<Float>
typealias IntRequester = Requester<Int>
typealias BoolRequester = Requester<Bool>
typealias StringRequester = Requester<String>
typealias DictionaryRequester = Requester<JSONDictionary>
typealias ArrayRequester = Requester<Array<JSONDictionary>>

public struct JSONData<D>: HandyJSON where D: HandyJSON {
    public var code = 0
    public var msg = ""
    public var rows: [D]?
    public var data: D?
    public init(){}
}

extension JSONData: Requestable{
    public typealias T = Self
}

extension APIHost {
    static let gushiwen: Self = "https://app.gushiwen.cn/"
    static let download: Self = "http://ultravideo.fi/video"
    static let upload: Self = ""
}

extension API {
    enum Gushiwen {
        case recommend(page: Int)
        case detail(id: String)
    }
}

extension API {
    enum Image: RequestAPI {
        case download(fullUrl: String)
        
        var request: Request {
            switch self {
            case .download(let fullUrl):
                return .init(host: .init(stringLiteral: fullUrl))
            }
        }
    }
    enum File: RequestAPI {
        var request: Request {
            switch self {
            case .downloadVideo(let name):
                let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
                return .init(method: .get, taskType: .downloadURL(savePath: path), host: APIHost.download, path: name)
            default:
                return .init(host: "")
            }
        }
        
        case downloadVideo(name: String)
        case upload
    }
}

extension API.Gushiwen: RequestAPI {
    var request: Request {
        switch self {
        case .recommend(let page):
            return .init(method: .get, host: .gushiwen, path: "/api/onehour/Default10.aspx?page=\(page)&token=gswapi")
        case .detail(let id):
            return .init(host: .gushiwen, path: "api/shiwen/shiwenv11.aspx?id=\(id)&token=gswapi")
        }
    }
}
